package com.ruoyi.web.controller.train;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.train.domain.JyTrainingSchool;
import com.ruoyi.train.domain.JyTrainingSchoolStaff;
import com.ruoyi.train.domain.JyTrainingSchoolUser;
import com.ruoyi.train.service.IJyTrainingSchoolService;
import com.ruoyi.train.service.IJyTrainingSchoolStaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 培训学校员工Controller
 *
 * @author aulbrother
 * @date 2024-04-06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/train/staff")
public class JyTrainingSchoolStaffController extends BaseController {
    private final IJyTrainingSchoolStaffService jyTrainingSchoolStaffService;
    private final IJyTrainingSchoolService jyTrainingSchoolService;

    /**
     * 查询培训学校员工列表
     */
    @PreAuthorize("@ss.hasPermi('train:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(JyTrainingSchoolStaff jyTrainingSchoolStaff) {
        startPage();
        List<JyTrainingSchoolStaff> list = jyTrainingSchoolStaffService.selectJyTrainingSchoolStaffList(jyTrainingSchoolStaff);

        if (CollectionUtil.isNotEmpty(list)) {
            List<Long> ids = CollStreamUtil.toList(list, JyTrainingSchoolStaff::getTrainingSchoolId);
            List<JyTrainingSchool> trainingSchools = jyTrainingSchoolService.selectJyTrainingSchoolByIds(ids);
            Map<Long, JyTrainingSchool> schoolMap = CollStreamUtil.toIdentityMap(trainingSchools, JyTrainingSchool::getId);

            list.forEach(v -> {
                JyTrainingSchool trainingSchool = schoolMap.get(v.getTrainingSchoolId());
                if (trainingSchool != null) {
                    v.setTrainingSchoolName(trainingSchool.getSchoolName());
                }
            });
        }

        return getDataTable(list);
    }

    /**
     * 导出培训学校员工列表
     */
    @PreAuthorize("@ss.hasPermi('train:staff:export')")
    @Log(title = "培训学校员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JyTrainingSchoolStaff jyTrainingSchoolStaff) throws IOException {
        List<JyTrainingSchoolStaff> list = jyTrainingSchoolStaffService.selectJyTrainingSchoolStaffList(jyTrainingSchoolStaff);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "培训学校员工数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JyTrainingSchoolStaff.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取培训学校员工详细信息
     */
    @PreAuthorize("@ss.hasPermi('train:staff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        JyTrainingSchoolStaff schoolStaff = jyTrainingSchoolStaffService.selectJyTrainingSchoolStaffById(id);

        if (null != schoolStaff && null != schoolStaff.getTrainingSchoolId()) {
            JyTrainingSchool trainingSchool = jyTrainingSchoolService.selectJyTrainingSchoolById(schoolStaff.getTrainingSchoolId());
            if (trainingSchool != null) {
                schoolStaff.setTrainingSchoolName(trainingSchool.getSchoolName());
            }
        }

        return success(schoolStaff);
    }
}
